﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace SerializationHelper
{
    public abstract class SerTool
    {
        protected string _path;
        protected virtual void SerializationObject<T>(T t, System.Runtime.Serialization.IFormatter serFormatter)
        {
            try
            {
                if (serFormatter == null)
                    throw new ArgumentNullException("serFormatter");
                using (Stream _stream = new FileStream(_path, FileMode.Create, FileAccess.Write))
                {
                    serFormatter.Serialize(_stream, t);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected virtual void SerializationCollectionToXML<T>(IList<T> collection)
        {
            try
            {
                using (Stream _stream = new FileStream(_path, FileMode.Create, FileAccess.Write))
                {
                    XmlTextWriter _xmlTextWriter = new XmlTextWriter(_stream, new UTF8Encoding(false));
                    _xmlTextWriter.Formatting = Formatting.Indented;
                    XmlSerializer _xmlSerializer = new XmlSerializer(collection.GetType());
                    _xmlSerializer.Serialize(_xmlTextWriter, collection);
                    _xmlTextWriter.Flush();
                    _xmlTextWriter.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected virtual void SerializationObjectToXML<T>(T t)
        {
            try
            {
                using (Stream _stream = new FileStream(_path, FileMode.Create, FileAccess.Write))
                {
                    XmlTextWriter _xmlTextWriter = new XmlTextWriter(_stream, new UTF8Encoding(false));
                    _xmlTextWriter.Formatting = Formatting.Indented;
                    XmlSerializer _xmlSerializer = new XmlSerializer(t.GetType());
                    _xmlSerializer.Serialize(_xmlTextWriter, t);
                    _xmlTextWriter.Flush();
                    _xmlTextWriter.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected virtual T DeSerializationXMLToObject<T>() where T : class
        {
            try
            {
                using (Stream _stream = new FileStream(_path, FileMode.Open, FileAccess.Read))
                {
                    XmlSerializer _xmlSerializer = new XmlSerializer(typeof(T));
                    return (T)_xmlSerializer.Deserialize(_stream);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected virtual T DeSerializationObject<T>(IFormatter serFormatter) where T : class
        {
            try
            {
                if (serFormatter == null)
                    throw new ArgumentNullException("SerFormatter");
                using (Stream _stream = new FileStream(_path, FileMode.Open, FileAccess.Read))
                {
                    return (T)serFormatter.Deserialize(_stream);
                }
            }
            catch
            {
                return null;
            }
        }
        public virtual void SerializationCollectionToBinary<T>(IList<T> collection)
        {
            try
            {
                IFormatter _serFormatter = new BinaryFormatter();
                using (Stream _stream = new FileStream(_path, FileMode.Create, FileAccess.Write))
                {
                    _serFormatter.Serialize(_stream, collection);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public virtual IList<T> DeSerializationBinaryToCollection<T>()
        {
            try
            {
                IFormatter _serFormatter = new BinaryFormatter();
                _serFormatter.Binder = new UBinder();
                using (Stream _stream = new FileStream(_path, FileMode.Open, FileAccess.Read))
                {
                    return (IList<T>)_serFormatter.Deserialize(_stream);
                }
            }
            catch
            {
                return null;
            }
        }
        protected virtual IList<T> DeSerializationXMLToCollection<T>()
        {
            try
            {
                using (Stream _stream = new FileStream(_path, FileMode.Open, FileAccess.Read))
                {
                    XmlSerializer _xmlSerializer = new XmlSerializer(typeof(List<T>));
                    return (IList<T>)_xmlSerializer.Deserialize(_stream);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
